package me.sauce.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import me.sauce.common.ChangeModel;
import me.sauce.common.LoginModel;
import me.sauce.common.SimpleResult;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * Created by sauce on 2017/4/24.
 */
@RestController
public class TestController {
    private Logger logger = Logger.getLogger(this.getClass());

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public JsonNode login(@RequestBody LoginModel mContext) {
        ObjectNode rootNode = JsonNodeFactory.instance.objectNode();
        logger.info("我就试试");
        if (mContext.getMobile().equals(mContext.getPassword()))
            // Data Service
            return rootNode.set("result", JsonNodeFactory.instance.pojoNode(new SimpleResult(200, "ok")));
        else
            return rootNode.set("result", JsonNodeFactory.instance.pojoNode(new SimpleResult(500, "fail")));

    }

    @RequestMapping(value = "/token", method = RequestMethod.GET)
    public JsonNode viewContract(@RequestParam(value = "userId", defaultValue = "0") int userId, HttpServletRequest request) {
        ObjectNode rootNode = JsonNodeFactory.instance.objectNode();
        rootNode.put("userId", userId + 100);
        return rootNode.set("result", JsonNodeFactory.instance.pojoNode(new SimpleResult(200, "ok")));

    }
    @RequestMapping(value = "/change", method = RequestMethod.PUT)
    public JsonNode change(@RequestBody ChangeModel changeModel) {
        ObjectNode rootNode = JsonNodeFactory.instance.objectNode();
        logger.info("我就试试改密码");
        if (changeModel.getNewPwd().equals(changeModel.getOldPwd()))
            // Data Service
            return rootNode.set("result", JsonNodeFactory.instance.pojoNode(new SimpleResult(200, "ok")));
        else
            return rootNode.set("result", JsonNodeFactory.instance.pojoNode(new SimpleResult(500, "fail")));

    }
}
